package gunfire.powers;

import basemod.cardmods.ExhaustMod;
import basemod.helpers.CardModifierManager;
import basemod.interfaces.CloneablePowerInterface;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.megacrit.cardcrawl.actions.common.MakeTempCardInHandAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardQueueItem;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.CardLibrary;
import com.megacrit.cardcrawl.localization.PowerStrings;
import com.megacrit.cardcrawl.powers.AbstractPower;
import gunfire.GunfireMod;
import gunfire.cards.DoubleOrb;
import gunfire.util.TextureLoader;

import java.util.ArrayList;

public class EnergyStoragePower extends AbstractPower implements CloneablePowerInterface {
    public AbstractCreature source;

    public static final String POWER_ID = GunfireMod.makeID("EnergyStoragePower");
    private static final PowerStrings powerStrings = CardCrawlGame.languagePack.getPowerStrings(POWER_ID);
    public static final String NAME = powerStrings.NAME;
    public static final String[] DESCRIPTIONS = powerStrings.DESCRIPTIONS;

    private static final Texture tex84 = TextureLoader.getTexture("gunfireResources/images/powers/EnergyStoragePower84.png");
    private static final Texture tex32 = TextureLoader.getTexture("gunfireResources/images/powers/EnergyStoragePower32.png");

    public EnergyStoragePower(AbstractCreature owner, final int amount) {
        name = NAME;
        ID = POWER_ID;

        this.owner = owner;
        this.amount = amount;

        type = PowerType.BUFF;

        this.region128 = new TextureAtlas.AtlasRegion(tex84, 0, 0, 84, 84);
        this.region48 = new TextureAtlas.AtlasRegion(tex32, 0, 0, 32, 32);

        updateDescription();
    }

    @Override
    public void updateDescription() {
        this.description = DESCRIPTIONS[0] + this.amount + DESCRIPTIONS[1];
    }

    @Override
    public void atStartOfTurnPostDraw() {
        this.flash();
        ArrayList<AbstractCard> qCardList = new ArrayList<>();
        for (AbstractCard r : CardLibrary.getAllCards()) {
            if (r.color == AbstractDungeon.player.getCardColor() && r.hasTag(GunfireMod.ENERGY_ORB_CARD)) {
                qCardList.add(r);
            }
        }

        for(int i = 0; i < this.amount; ++i) {
            AbstractCard c = qCardList.get(AbstractDungeon.cardRandomRng.random(qCardList.size() - 1)).makeCopy();
            CardModifierManager.addModifier(c, new ExhaustMod());
            this.addToBot(new MakeTempCardInHandAction(c, 1, false));
        }
    }

    @Override
    public AbstractPower makeCopy() {
        return new EnergyStoragePower(owner, amount);
    }
}
